package com.cskaoyan.servlet;


import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.data.ResDeleteData;
import com.cskaoyan.bean.data.ResRefundData;
import com.cskaoyan.bean.data.ResShipData;
import com.cskaoyan.bean.data.RespOrderDetail;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.OrderServiceImpl;
import com.cskaoyan.util.*;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@WebServlet("/admin/order/*")
public class AdminOrderServlet extends WdBaseServlet {
    //全部order方法共享

    @SneakyThrows
    public void list(HttpServletRequest request, HttpServletResponse response) {
        OrderService orderService = new OrderServiceImpl();
        //封装订单信息数据
        //接收userId为空赋值为null，否则转化为Integer类型
        Integer userId = null;

        if (request.getParameter("userId") != null && !"".equals(request.getParameter("userId"))) {
            userId = Integer.parseInt(request.getParameter("userId"));
        }
        String orderSn = null;
        if (request.getParameter("orderSn") != null && !"".equals(request.getParameter("orderSn"))){
            orderSn=request.getParameter("orderSn");

        }
        SimpleDateFormat format = new SimpleDateFormat(WdConstant.DEFAULT_DATETIME_PATTERN);
        Date start = StringUtil.isEmpty(request.getParameter("start")) ? null :
                format.parse(request.getParameter("start"));
        Date end = StringUtil.isEmpty(request.getParameter("end")) ? null :
                format.parse(request.getParameter("end"));

        //这段部分不懂时什么意思
        List<Short> orderStatusArray = new ArrayList<>();
        String[] orderStatusArrays = request.getParameterValues("orderStatusArray");
        if (!ArrayUtil.isEmpty(orderStatusArrays)) {
            orderStatusArray = Arrays.asList(orderStatusArrays).stream().map(Short::valueOf).collect(Collectors.toList());
        }

        //封装页面查询信息
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        CommonData<MarketOrder> commonData = orderService.list(userId, orderSn, start, end, orderStatusArray, page, limit, sort, order);
        ResponseUtil.responseJson(response, BaseRespVo.ok(commonData));
    }

    public BaseRespVo detail(HttpServletRequest request, HttpServletResponse response) {
        OrderService orderService = new OrderServiceImpl();
        //获取请求参数
        Integer id = request.getParameter("id") == null
                ? null : Integer.parseInt(request.getParameter("id"));
        //orderService访问数据库
        RespOrderDetail orderDetail = orderService.detail(id);
        BaseRespVo baseRespVo = BaseRespVo.ok(orderDetail);
        return baseRespVo;
    }

    @SneakyThrows
    public BaseRespVo refund(HttpServletRequest request, HttpServletResponse response) {
        OrderService orderService = new OrderServiceImpl();
        //获取请求参数
        String jsonStr = request.getReader().readLine();
        ResRefundData resRefundData = JacksonUtil.read(jsonStr, ResRefundData.class);
        //调用service
        boolean flag = orderService.refund(resRefundData);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if (flag == true) {
            //wyy修改以下一行，为日志服务
            return BaseRespVo.ok(resRefundData);
        } else {
            return baseRespVo;
        }
    }

    BaseRespVo channel(HttpServletRequest request, HttpServletResponse response) {
        //存放12个Map，map里存放code name键值对
        int index = 0;
        List<Map<String, String>> vendors = VendorsProperties.getVendors();

        return BaseRespVo.ok(vendors);
    }

    @SneakyThrows
    BaseRespVo ship(HttpServletRequest request, HttpServletResponse response) {
        OrderService orderService = new OrderServiceImpl();
        //获取参数
        String jsonStr = request.getReader().readLine();
        ResShipData resShipData = JacksonUtil.read(jsonStr, ResShipData.class);
        if(resShipData.getShipChannel()==null){
            return BaseRespVo.topicNo();
        }
        //调用service 根据orderId查询对应订单
        orderService.ship(resShipData);

        //wyy修改以下一行，为日志服务
        return BaseRespVo.ok(resShipData);
    }

    @SneakyThrows
    BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        OrderService orderService = new OrderServiceImpl();
        String jsonStr = request.getReader().readLine();
        ResDeleteData resDeleteData = JacksonUtil.read(jsonStr, ResDeleteData.class);
        Short status =orderService.getStatus(resDeleteData.getOrderId());
        if(status == 203 || status == 401 || status == 402) {
            orderService.delete(resDeleteData.getOrderId());
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail(623,"订单不能删除");
        }
    }

    @SneakyThrows
    BaseRespVo reply(HttpServletRequest request, HttpServletResponse response){
        String s = request.getReader().readLine();

        return BaseRespVo.ok();

    }
}
